package com.nf.health.app.homepage.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.SchemeAdapter;
import com.nf.health.app.adapter.SchemeSystemAdapter;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.mine.activity.HealthSchemeActivity;
import com.nf.health.app.mine.activity.HealthSchemeSystemActivity;
import com.nf.health.app.models.SchemeAddVo;
import com.nf.health.app.models.SchemeVo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 首页健康方案添加
 * Created by windoes on 2016/3/16.
 */
public class SystemSchemeActivity extends BaseActivity {

    @Bind(R.id.scheme_listview)
    DragListView schemeListview;

    List<SchemeAddVo> mlistwwc = new ArrayList<SchemeAddVo>();//未完成

    private SchemeSystemAdapter schemeAdapter;

    private int pageNo = 1, pageSize = 20;
    private int firstItemPos = 0;//第一条显示的位置,在返回时显示此次位置,防止因刷新导致显示第一条
    List<SchemeVo> wwc_mlist;

    View headerView;

    MyListView myListView;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_scheme);
        ButterKnife.bind(this);


        headerView = getLayoutInflater().inflate(R.layout.layout_mylistview, null);

        myListView = (MyListView) headerView.findViewById(R.id.scheme_listview1);


        schemeListview.addHeaderView(headerView);


        ViewUtil.setText(this, R.id.titlebar_title_tv, "健康方案");

        schemeListview.setRefreshableAndLoadMoreable(true, true);

        schemeListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageNo = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                initData("more");
            }
        });
        schemeListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("detail", mlistwwc.get(position-2).getDetail());
                bundle.putString("totaldaynum", mlistwwc.get(position-2).getTotaldaynum());//共有
                bundle.putString("name", mlistwwc.get(position-2).getName());//名称
                bundle.putString("id", mlistwwc.get(position-2).getId());//id
                ActivityUtils.showActivity(SystemSchemeActivity.this, HealthSchemeSystemActivity.class, bundle);
                firstItemPos = schemeListview.getFirstVisiblePosition();
            }
        });

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("detail", wwc_mlist.get(position).getDetail());
                bundle.putString("totaldaynum", wwc_mlist.get(position).getTotaldaynum());//共有
                bundle.putString("daynum", wwc_mlist.get(position).getDaynum());//已完成
                bundle.putString("name", wwc_mlist.get(position).getName());//名称
                bundle.putString("id", wwc_mlist.get(position).getId());//id
                bundle.putString("planids", wwc_mlist.get(position).getPlanids());//planids
                bundle.putString("everydaynum", wwc_mlist.get(position).getEverydaynum());//everydaynum
                bundle.putString("schemetempletid", wwc_mlist.get(position).getSchemetempletid());//everydaynum
                ActivityUtils.showActivity(SystemSchemeActivity.this, HealthSchemeActivity.class, bundle);
            }
        });
    }

    //返回结果
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (actionName.equals("refresh")) {
            schemeListview.completeRefresh();
            mlistwwc.clear();
            List<SchemeAddVo> mlist = (List<SchemeAddVo>) object;
            mlistwwc.addAll(mlist);
            schemeAdapter = new SchemeSystemAdapter(this, mlistwwc);
            schemeListview.setAdapter(schemeAdapter);
            schemeAdapter.notifyDataSetChanged();
            schemeListview.setSelection(firstItemPos);
        }else if(actionName.equals("more")){
            schemeListview.completeLoadMore();

            List<SchemeAddVo> mlist = (List<SchemeAddVo>) object;
            if(mlist.size() == 0){
                showToast("没有更多数据");
                pageNo = 1 ;
            }else{
                mlistwwc.addAll(mlist);
                schemeAdapter.notifyDataSetChanged();
            }

        }else if(actionName.equals("wwc")){
            wwc_mlist = (List<SchemeVo>) object;
            SchemeAdapter schemeAdapter = new SchemeAdapter(this,wwc_mlist);
            myListView.setAdapter(schemeAdapter);

        }
    }

    protected void onResume() {
        super.onResume();
        initData("refresh");
    }

    private void initData(String flag) {
        provider.requestWWCHealthSchemeUserList("wwc");//请求方案列表
        provider.requestXXHealthSchemeUserList(pageNo + "", pageSize + "", flag);//请求方案列表
    }
}
